package com.qianzhou.eduWeb.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.qianzhou.dto.UserCourseOrderDTO;
import com.qianzhou.eduService.service.UserCourseOrderService;
import com.qianzhou.vo.ResponseVO;
import com.qianzhou.vo.UserCourseOrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: qian.zhou
 * @Description: 用户订单接口
 * @Date Created in 2021-08-29-16:35
 * @Modified By:
 */
@Slf4j
@RestController
@RequestMapping("order")
@Api(tags = {"用户订单接口"})
public class UserCourseOrderController {
    @Reference
    private UserCourseOrderService userCourseOrderService;

    @ApiOperation("保存订单/新增订单")
    @RequestMapping(value = "saveOrder", method = {RequestMethod.POST})
    public ResponseVO<Object> saveOrder(@RequestBody UserCourseOrderDTO dto) {
        userCourseOrderService.saveOrder(dto);
        return new ResponseVO<>(2000, "响应成功", dto.getOrderNo());
    }

    @ApiOperation("更新修改订单")
    @RequestMapping(value = "updateOrder/{orderNo}/{status}", method = {RequestMethod.POST})
    public ResponseVO<Object> updateOrder(@PathVariable("orderNo") String orderNo,
                                          @PathVariable("status") Byte status) {
        if (userCourseOrderService.updateOrder(orderNo, status) == null) {
            return new ResponseVO<>(5000, "修改订单失败", null);
        } else {
            return new ResponseVO<>(2000, "修改订单成功", null);
        }
    }

    @ApiOperation("删除订单")
    @RequestMapping(value = "deleteOrder/{orderNo}", method = {RequestMethod.POST})
    public ResponseVO<Object> deleteOrder(@PathVariable("orderNo") String orderNo) {
        if (userCourseOrderService.deleteOrder(orderNo) == null) {
            return new ResponseVO<>(5000, "删除订单失败", null);
        } else {
            return new ResponseVO<>(2000, "删除订单成功", null);
        }
    }

    @ApiOperation("获取用户订单")
    @RequestMapping(value = "getOrdersByUserId/{userId}", method = {RequestMethod.GET})
    public ResponseVO<Object> getOrdersByUserId(@PathVariable("userId") String userId) {
        List<UserCourseOrderVO> courseOrderVOS = userCourseOrderService.getOrdersByUserId(userId);
        return new ResponseVO<>(2000, "响应成功", courseOrderVOS);
    }
}
